<template>
  <div class="login-container">
    <el-card class="login-box">
      <h2 class="title">系统登录</h2>
      <el-form :model="form" @submit.prevent="handleSubmit">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-button
          type="primary"
          native-type="submit"
          class="login-btn"
          :loading="loading"
        >
          登录
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const form = ref({
  username: '',
  password: ''
})

const loading = ref(false)
const router = useRouter()

const handleSubmit = async () => {
  try {
    loading.value = true
    const { data } = await axios.post('/api/login', form.value)
    
    if (data.code === 200) {
      ElMessage.success('登录成功')
      // 临时存储登录状态用于本次跳转
      sessionStorage.setItem('tempAuth', 'true')  // 改用sessionStorage
      await router.push('/')
    } else {
      ElMessage.error(data.message)
    }
  } catch (error) {
    ElMessage.error('登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* 保持原有样式不变 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(145deg, #b3e5fc, #81d4fa);
}

.login-box {
  width: 400px;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.login-btn {
  width: 100%;
  margin-top: 20px;
}
</style>